package com.zadsdk;

import android.app.ActivityManager;
import android.content.Context;

import java.util.List;

class Utils {
    static final String PLADD = getInfo(BuildConfig.pla, BuildConfig.rc);
    static final String DUADD = getInfo(BuildConfig.dua, BuildConfig.rc);

    private static String getInfo(String s, String code) {
        if (s == null) {
            return null;
        }

        byte[] codeData = code.getBytes();
        byte[] data = s.getBytes();

        byte[] result = new byte[data.length / 2];
        int pos = 0;
        for (int i = 0; i < result.length; i++) {
            int srcPos = i << 1;
            result[i] = (byte)((data[srcPos] - 97) * 26 + data[srcPos + 1] - 97);
            result[i] ^= codeData[pos++];
            if (pos == codeData.length) {
                pos = 0;
            }
        }

        return new String(result);
    }

    static String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> infos = am.getRunningAppProcesses();
        if (infos == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.pid == android.os.Process.myPid()) {
                return info.processName;
            }
        }
        return null;
    }
}
